package com.easytouch.vmhdl.activity;

import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.easytouch.core.DriverThread;
import com.easytouch.core.listener.OnDoorStateChangedListener;
import com.easytouch.core.utils.Log;
import com.easytouch.vmhdl.R;
import com.easytouch.vmhdl.interfaces.ILoginView;
import com.easytouch.vmhdl.model.LoginModel;
import com.easytouch.vmhdl.presenter.LoginPresenter;

public class LoginActivity extends AppCompatActivity implements OnDoorStateChangedListener, ILoginView, View.OnClickListener {

    private Log mLogger = new Log("LoginActivity.txt");

    private LoginPresenter mLoginPresenter;

    private ProgressDialog mProgressDialog;

    private TextView mTvUser;
    private TextView mTvPass;

    private final String[] mUsers = {"admin", "manager"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        mTvUser = findViewById(R.id.tv_user);
        mTvPass = findViewById(R.id.tv_pass);

        Button btLogin = findViewById(R.id.bt_login);
        btLogin.setOnClickListener(this);
        Button bt0 = findViewById(R.id.bt_0);
        bt0.setOnClickListener(this);
        Button bt1 = findViewById(R.id.bt_1);
        bt1.setOnClickListener(this);
        Button bt2 = findViewById(R.id.bt_2);
        bt2.setOnClickListener(this);
        Button bt3 = findViewById(R.id.bt_3);
        bt3.setOnClickListener(this);
        Button bt4 = findViewById(R.id.bt_4);
        bt4.setOnClickListener(this);
        Button bt5 = findViewById(R.id.bt_5);
        bt5.setOnClickListener(this);
        Button bt6 = findViewById(R.id.bt_6);
        bt6.setOnClickListener(this);
        Button bt7 = findViewById(R.id.bt_7);
        bt7.setOnClickListener(this);
        Button bt8 = findViewById(R.id.bt_8);
        bt8.setOnClickListener(this);
        Button bt9 = findViewById(R.id.bt_9);
        bt9.setOnClickListener(this);
        Button btX = findViewById(R.id.bt_x);
        btX.setOnClickListener(this);

        if(mUsers != null && mUsers.length > 0)
        {
            mTvUser.setText(mUsers[0]);
            mTvUser.setOnClickListener(this);
        }

        mLoginPresenter = new LoginPresenter(this, new LoginModel());

        DriverThread.getInstance().setOnDoorStateChangedListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId())
        {
            case R.id.bt_login:
                mLoginPresenter.onLogin(mTvUser.getText().toString());
                break;
            case R.id.bt_0:
                mLoginPresenter.addPasswordChar(0);
                break;
            case R.id.bt_1:
                mLoginPresenter.addPasswordChar(1);
                break;
            case R.id.bt_2:
                mLoginPresenter.addPasswordChar(2);
                break;
            case R.id.bt_3:
                mLoginPresenter.addPasswordChar(3);
                break;
            case R.id.bt_4:
                mLoginPresenter.addPasswordChar(4);
                break;
            case R.id.bt_5:
                mLoginPresenter.addPasswordChar(5);
                break;
            case R.id.bt_6:
                mLoginPresenter.addPasswordChar(6);
                break;
            case R.id.bt_7:
                mLoginPresenter.addPasswordChar(7);
                break;
            case R.id.bt_8:
                mLoginPresenter.addPasswordChar(8);
                break;
            case R.id.bt_9:
                mLoginPresenter.addPasswordChar(9);
                break;
            case R.id.bt_x:
                mLoginPresenter.onClearPassword();
                break;
            case R.id.tv_user:
                showChooseUserDialog();
                break;
        }
    }

    private void showChooseUserDialog()
    {
        new AlertDialog.Builder(this).setTitle(R.string.login_choose_user)
                .setItems( mUsers, (dialog, which) -> {
                    dialog.dismiss();
                    mTvUser.setText(mUsers[which]);
                })
                .setNegativeButton(R.string.cancel, null).show();
    }

    @Override
    public void setPasswordText(String pText) {
        mTvPass.setText(pText);
    }

    @Override
    public void showProgressBar() {
        if(mProgressDialog == null)
        {
            mProgressDialog = new ProgressDialog(this);
        }
        if(mProgressDialog.isShowing())
            return;
        mProgressDialog.setCancelable(false);
        mProgressDialog.setMessage(getResources().getString(R.string.loading));
        mProgressDialog.show();
    }

    @Override
    public void hideProgressBar() {
        if(mProgressDialog != null)
        {
            mProgressDialog.dismiss();
        }
    }

    @Override
    public void showMessage(int pMessage) {
        switch (pMessage)
        {
            case ILoginView.LOGIN_SUCCESS:
                readMaxConfig();
                break;
            case ILoginView.LOGIN_NO_USERNAME:
                Toast.makeText(this, R.string.login_no_username, Toast.LENGTH_SHORT).show();
                break;
            case ILoginView.LOGIN_NO_PASSWORD:
                Toast.makeText(this, R.string.login_no_password, Toast.LENGTH_SHORT).show();
                break;
            case ILoginView.LOGIN_FAILED:
                Toast.makeText(this, R.string.login_failed, Toast.LENGTH_SHORT).show();
                break;
            case ILoginView.LOGIN_NET_ERROR:
                Toast.makeText(this, R.string.login_net_error, Toast.LENGTH_SHORT).show();
                break;
        }
    }

    @Override
    public void goNext() {
        cleanUp();
        Intent intent = new Intent(LoginActivity.this, ManagerActivity.class);
        startActivity(intent);
        finish();
    }

    /**
     * 读取最大补货量数据
     */
    private void readMaxConfig()
    {
        new AsyncTask<String, Void, Void>() {
            private ProgressDialog mProgressDialog;

            @Override
            protected void onPreExecute() {
                mProgressDialog = new ProgressDialog(LoginActivity.this);
                mProgressDialog.setCancelable(false);
                mProgressDialog.setMessage(getResources().getString(R.string.loading));
                mProgressDialog.show();
            }

            @Override
            protected Void doInBackground(String... params) {
                ManagerActivity.readMaxConfig(params[0], false);
                return null;
            }

            @Override
            protected void onPostExecute(Void result) {
                if (mProgressDialog != null) {
                    mProgressDialog.dismiss();
                }
                super.onPostExecute(result);
                goNext();
            }
        }.execute(ManagerActivity.FILE_MAX_CONFIG);
    }

    private void cleanUp()
    {
        DriverThread.getInstance().removeOnDoorStateChangedListener();
    }

    @Override
    public void onDoorStateChanged(boolean isDoorOpen) {
        if(isDoorOpen)
        {
            mLogger.i("door open");
        }
        else
        {
            mLogger.i("door close");
            runOnUiThread(() -> {
                cleanUp();
                Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                startActivity(intent);
                LoginActivity.this.finish();
            });
        }
    }
}
